#!/usr/bin/perl

use NetAddr::IP;
use Net::IP;
use File::Basename;

use strict;

use FindBin '$Bin';
use lib "$Bin/lib";
use utils;
use Discover;

my $t = new Discover;
my ($key, $format) = ("-f", "text");
my $result;

my $COMMAND = $ARGV[0];
if($COMMAND =~ m/^-f*/){
    ($key, $format) = split("@|:", $COMMAND);
    shift (@ARGV);
    $COMMAND = $ARGV[0];
    print Dumper($ARGV);
}

if($COMMAND eq "discover"){
    my ($save, $discover) = (1, 1, 0);
    my ($community, $net);
    for(my $i = 1; $i <= $#ARGV; $i++){
	($community, $net) = split("@|:", $ARGV[$i]);
	if ($community eq "-t") { # test only - do not save
	    $save = 0;
	    next;
	}
	if ($community eq "-v") { # set GLOBAL VPN (default 0)
	    $t->{defaultVPN} = $net;
	    next;
	}
	if ($community eq "-m") { # set GLOBAL VPN (default 0)
	    $t->{doMTable} = 1;
	    next;
	}

	my @comms = split /\|/, $community;
	$t->discover(new Net::IP($net), @comms);
	$t->clean();
	$t->save();

	# Resetting to default values
	$t->{doMTable} = 0;
	$t->{defaultVPN} = 0;
	$save = 1;
    }
    exit 0;
} elsif($COMMAND eq "scan"){
    my ($iscope_regexp, $ialias_regexp) = ('(con|mtun|ipip|lsi|demux|pip|lo|tap|pp|cbp|pime|me|dsc|irb|gre|lc|pimd|ae|null|aux|vlan)', '^[0-9A-z]+:.+');
    for(my $i = 1; $i <= $#ARGV; $i++){
	my ($key, $value) = split("@|:", $ARGV[$i]);
	if ($key eq "-i") { 
	    $iscope_regexp = $value;
	    next;
	}
	if ($key eq "-a") { 
	    $ialias_regexp = $value;
	    next;
	}
    }
    $result = $t->wreport($iscope_regexp, $ialias_regexp);
} elsif($COMMAND eq "domain"){
    my ($zone, $ifregexp, $scope, $header, $reverse) = ("some.net", "[\s\/\.\:]", new NetAddr::IP("0.0.0.0/0"), 1, 0);
    for(my $i = 1; $i <= $#ARGV; $i++){
	my ($key, $value) = split("@|:", $ARGV[$i]);
	if ($key eq "-z") { 
	    $zone = $value;
	    next;
	}
	if ($key eq "-s") { 
	    $scope = new NetAddr::IP($value);
	    next;
	}

	if ($key eq "-h") { 
	    $header = 0;
	    next;
	}

	if ($key eq "-r") { 
	    $ifregexp = $value;
	    next;
	}
	
	if ($key eq "-a") { 
	    $reverse = 1;
	    next;
	}
    }
    $t->domain($zone, $ifregexp, $scope, $header, $reverse);
} elsif($COMMAND eq "info"){
    my ($exact, $showarp) = (0, 0);
    my $i = 1;
    for(; $i <= $#ARGV; $i++){
	my ($key, $value) = split("@|:", $ARGV[$i]);
	if ($key eq "-e") { 
	    $exact = 1;
	    next;
	}
	if ($key eq "-a") { 
	    $showarp = 1;
	    next;
	}
    }
    our $ip = $ARGV[$i - 1];
    $t->info($ip, $exact, $showarp);
} elsif($COMMAND eq "bgp"){
    my ($exclude, $include, $detail, $format) = ("_", ".*", 0, 0);
    my $i = 1;
    for(; $i <= $#ARGV; $i++){
	my ($key, $value) = split("@|:", $ARGV[$i]);
	if ($key eq "-e") { 
	    $exclude = $value;
	    next;
	}
	if ($key eq "-i") { 
	    $include = $value;
	    next;
	}

	if ($key eq "-d") { 
	    $detail = 1;
	    next;
	}
	our $ip = $ARGV[$i];
	
    }
    $result = $t->bgp($exclude, $include, $detail);
} elsif($COMMAND eq "vrf"){
    my $name = $ARGV[1];
    $t->vrf($name);
} elsif($COMMAND eq "usage"){
    my($free) = (0);
    my $i = 1;
    for(; $i <= $#ARGV; $i++){
	my ($key, $value) = split("@|:", $ARGV[$i]);
	if ($key eq "-f") { 
	    $free = 1;
	    next;
	}
	if ($key eq "-fo") { 
	    $free = 2;
	    next;
	}
	last;
    }
    $result = $t->usage(new NetAddr::IP($ARGV[$i]), $free);
}

if($result != undef){
    if($format eq "json"){
	Discover::json($result);
    }
    else {
	Discover::output($result);
    }
}

